//
//  HaodianCell.m
//  HomeIn
//
//  Created by dllo on 15/11/10.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "HaodianCell.h"
#import "HaodianModel.h"
#import "TagCell.h"

@implementation HaodianCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
    
    
    //头像
    self.dianpuImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, WIDTH / 5, WIDTH / 5)];
    self.dianpuImageView.layer.masksToBounds = YES;
    self.dianpuImageView.layer.cornerRadius = self.dianpuImageView.frame.size.width / 2;//圆形半径是(正方形)宽度的一半
    self.dianpuImageView.layer.borderWidth = 1;//设置圆角边框的厚度
    self.dianpuImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;//设置边框颜色
    [self.contentView addSubview:self.dianpuImageView];

    
    //名字
    self.shopNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 + self.dianpuImageView.frame.size.width + 5, self.dianpuImageView.frame.origin.y + 10, WIDTH / 2, (HEIGHT - 113) / 2 / 9)];
    [self.contentView addSubview:self.shopNameLabel];

    
    //标语
    self.shopSlogonLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.shopNameLabel.frame.origin.x, self.shopNameLabel.frame.origin.y + self.shopNameLabel.frame.size.height, WIDTH / 2, self.shopNameLabel.frame.size.height)];
    [self.contentView addSubview:self.shopSlogonLabel];

    //进入按钮
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(WIDTH - WIDTH / 5, self.shopNameLabel.frame.origin.y, WIDTH / 5 - 10, self.shopNameLabel.frame.size.height);
    [self.button setTitle:@"进入" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.button.backgroundColor = RGBA(251, 106, 10, 1);
    //masksToBounds裁剪掉button的边框
    self.button.layer.masksToBounds = YES;
    //设置圆角度
    self.button.layer.cornerRadius = 5;
    [self.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.button];
    

    //图片
    self.ImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.dianpuImageView.frame.origin.y + self.dianpuImageView.frame.size.height + 10, (WIDTH - 40) / 3, (WIDTH - 40) / 3)];
    self.ImageView1.userInteractionEnabled = YES;//用户交互
    [self.contentView addSubview:self.ImageView1];
    
    self.ImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(self.ImageView1.frame.origin.x + self.ImageView1.frame.size.width + 10, self.ImageView1.frame.origin.y, self.ImageView1.frame.size.width, self.ImageView1.frame.size.height)];
    self.ImageView2.userInteractionEnabled = YES;//用户交互
    [self.contentView addSubview:self.ImageView2];
    
    self.ImageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(self.ImageView2.frame.origin.x + self.ImageView2.frame.size.width + 10, self.ImageView2.frame.origin.y, self.ImageView2.frame.size.width, self.ImageView2.frame.size.height)];
    self.ImageView3.userInteractionEnabled = YES;//用户交互
    [self.contentView addSubview:self.ImageView3];
    
    //价格
    self.priceLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(10, self.ImageView1.frame.origin.y + self.ImageView1.frame.size.height, self.ImageView1.frame.size.width, self.shopNameLabel.frame.size.height)];
    [self.contentView addSubview:self.priceLabel1];
    
    self.priceLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(self.priceLabel1.frame.origin.x + self.priceLabel1.frame.size.width + 10, self.priceLabel1.frame.origin.y , self.priceLabel1.frame.size.width, self.priceLabel1.frame.size.height)];
    [self.contentView addSubview:self.priceLabel2];
    
    self.priceLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(self.priceLabel2.frame.origin.x + self.priceLabel2.frame.size.width + 10, self.priceLabel2.frame.origin.y , self.priceLabel2.frame.size.width, self.priceLabel2.frame.size.height)];
    [self.contentView addSubview:self.priceLabel3];
    

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置item大小
    flowLayout.itemSize = CGSizeMake((WIDTH - 45) / 6, self.shopNameLabel.frame.size.height);
    //设置item的行间距
    flowLayout.minimumLineSpacing = 5;
    //设置item的上,左,下,右 边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    //滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10 , self.priceLabel1.frame.origin.y + self.priceLabel1.frame.size.height + 5, WIDTH, self.shopNameLabel.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:self.collectionView];
    //注册cell
    [self.collectionView registerClass:[TagCell class] forCellWithReuseIdentifier:@"reuse"];
 
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.haodianModel.tags.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //直接从cell重用池里取
    TagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
   
    cell.layer.masksToBounds = YES;//masksToBounds裁剪掉边框
    cell.layer.cornerRadius = 5;//设置圆角度
    
    NSString *str = [[self.haodianModel.tags objectAtIndex:indexPath.row] objectForKey:@"app_name"];

    [cell setCellWithStr:str];//方法传值 (属性传值重写setter方法需要重新调整,别用)
    
    return cell;
}


- (void)buttonAction
{
    
    [self.MyDelegate pushToWeb:self.haodianModel];
   
}

- (void)setHaodianModel:(HaodianModel *)haodianModel
{
    _haodianModel = haodianModel;

    [self.dianpuImageView sd_setImageWithURL:[NSURL URLWithString:_haodianModel.picUrl]];

    self.shopNameLabel.text = _haodianModel.shopName;
    
    self.shopSlogonLabel.text = _haodianModel.shopSlogon;
    self.shopSlogonLabel.textColor = [UIColor lightGrayColor];
    self.shopSlogonLabel.font = [UIFont systemFontOfSize:15];
    
    [self.ImageView1 sd_setImageWithURL:[NSURL URLWithString:[[haodianModel.products objectAtIndex:0] objectForKey:@"image"]]];
    [self.ImageView2 sd_setImageWithURL:[NSURL URLWithString:[[haodianModel.products objectAtIndex:1] objectForKey:@"image"]]];
    [self.ImageView3 sd_setImageWithURL:[NSURL URLWithString:[[haodianModel.products objectAtIndex:2] objectForKey:@"image"]]];
    
    self.priceLabel1.text = [NSString stringWithFormat:@"%@元", [[haodianModel.products objectAtIndex:0] objectForKey:@"price"]];
    self.priceLabel1.textColor = [UIColor redColor];
    self.priceLabel2.text = [NSString stringWithFormat:@"%@元", [[haodianModel.products objectAtIndex:1] objectForKey:@"price"]];
    self.priceLabel2.textColor = [UIColor redColor];
    self.priceLabel3.text = [NSString stringWithFormat:@"%@元", [[haodianModel.products objectAtIndex:2] objectForKey:@"price"]];
    self.priceLabel3.textColor = [UIColor redColor];

    
    [self.collectionView reloadData];//刷新
      
  
}

@end
